package com.zmx.patients.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HealthDetectionActivity extends BaseActivity {

    @BindView(R.id.release_quantity)
    TextView releaseQuantity;
    @BindView(R.id.release_quantity_text)
    TextView releaseQuantityText;
    @BindView(R.id.relative_a)
    RelativeLayout relativeA;
    @BindView(R.id.clinch_a_deal)
    TextView clinchADeal;
    @BindView(R.id.clinch_a_deal_text)
    TextView clinchADealText;
    @BindView(R.id.relative_b)
    RelativeLayout relativeB;
    @BindView(R.id.closing)
    TextView closing;
    @BindView(R.id.closing_text)
    TextView closingText;
    @BindView(R.id.relative_c)
    RelativeLayout relativeC;
    @BindView(R.id.order)
    TextView order;
    @BindView(R.id.order_text)
    TextView orderText;
    @BindView(R.id.relative_d)
    RelativeLayout relativeD;
    @BindView(R.id.time_text1)
    TextView timeText1;
    @BindView(R.id.time_text2)
    TextView timeText2;
    @BindView(R.id.time_text3)
    TextView timeText3;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.bracelet)
    LinearLayout bracelet;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tcmknowledge;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        prevent();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:


                    try {

                        Log.e("我是定位信息", "我是定位" + msg.obj.toString());

                        JSONObject object = new JSONObject(msg.obj.toString());

                        if (object.getInt("code") == 0) {

                            Toast("未绑定手环，点击绑定");

                        } else {

                            JSONObject data = object.getJSONObject("data");


                            JSONObject bt = data.getJSONObject("bt");
                            releaseQuantity.setText("体温" + bt.getString("temp") + "°C");
                            releaseQuantityText.setText(bt.getString("tips"));

                            JSONObject hr = data.getJSONObject("hr");
                            clinchADeal.setText("血氧" + hr.getString("hr") + "ml");
                            clinchADealText.setText(hr.getString("tips"));

                            JSONObject bp = data.getJSONObject("bp");
                            order.setText("血压" + bp.getString("systolic")+"/"+bp.getString("diastolic"));
                            orderText.setText(bp.getString("tips"));


                            JSONObject oxygen = data.getJSONObject("oxygen");
                            closing.setText("心率" + oxygen.getString("value")+"次/分");
                            closingText.setText(oxygen.getString("tips"));

                            JSONObject timeshow = data.getJSONObject("timeshow");

                            timeText1.setText(timeshow.getString("name"));
                            timeText2.setText(timeshow.getString("time"));
                            timeText3.setText(timeshow.getString("msg"));

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case 3:
                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());


                        if (jsonObject.getInt("code") == 1){


                            Bundle bundles = new Bundle();

                            if (i_type == 0) {

                                bundles.putString("name",MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.name, ""));
                                bundles.putString("head",MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.avatar, ""));
                                bundles.putString("p_id",MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
                                bundles.putString("data",jsonObject.getString("data"));
                                startActivity(PulseConditionActivity.class,bundles);

                            } else {

                                bundles.putString("p_id", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
                                startActivity(BraceletInformationActivity.class, bundles);

                            }



                        }else{

                            Toast(jsonObject.getString("msg"));

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    public void prevent() {

        List<String> l = new ArrayList<>();
        l.add(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "prevent");
        params.put("data[patient_id]", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch", params, handler, 1, 404);

    }


    @OnClick({R.id.relative_a, R.id.relative_b, R.id.relative_c, R.id.relative_d,R.id.button,R.id.bracelet})
    public void onViewClicked(View view) {

        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.relative_a:

                bundle.putString("logo","a");
                startActivity(BodyTemperatureActivity.class,bundle);


                break;
            case R.id.relative_b:

                bundle.putString("logo","b");
                startActivity(BodyTemperatureActivity.class,bundle);

                break;
            case R.id.relative_c:

                bundle.putString("logo","b");
                startActivity(BodyTemperatureActivity.class,bundle);

                break;
            case R.id.relative_d:

                bundle.putString("logo","d");
                startActivity(BodyTemperatureActivity.class,bundle);
                break;

            case R.id.button:
                getPulesStatus();
                break;

            case R.id.bracelet:
                i_type = 1;
                getPulesStatus();

                break;
        }
    }


    private Integer i_type = 0;
    /**
     * 检测脉象
     */
    public void getPulesStatus() {

        List<String> ls = new ArrayList<>();
        ls.add("5");
        ls.add(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getPulesStatus");
        params.put("data[patient_id]", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("data[minute]", "5");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch", params, handler, 3, 404);

    }

}
